home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 586 < prev    next >
Encoding:
Text File  |  1996-08-05  |  7.0 KB  |  228 lines

  1. Path: luxor.latrobe.edu.au!csigjb
  2. From: csigjb@luxor.latrobe.edu.au ()
  3. Newsgroups: alt.msdos.programmer,comp.lang.c
  4. Subject: Two C problems
  5. Date: 7 Jan 1996 14:01:27 GMT
  6. Organization: La Trobe University
  7. Distribution: world
  8. Message-ID: <4cojnn$rgd@lugb.latrobe.edu.au>
  9. NNTP-Posting-Host: luxor.latrobe.edu.au
  10. Originator: csigjb@luxor.latrobe.edu.au
  11.  
  12. I have two C problems.
  13.  
  14. PROBLEM 1 :
  15.  
  16. int main(int argc,char *argv[])
  17. {
  18.      FILE *file;
  19.      char x,ch,width,height,back,fore,stringpos;
  20.      int numlines,linenum;
  21.      text_info info;
  22.      nodetypeptr listptr,posptr;
  23.  
  24.  
  25.      if (argc<2)
  26.      {
  27.           cputs("\nUsage : show file name [fore ground color] [back ground color] . . .\n");
  28.      }
  29.      else
  30.      {
  31.           file=fopen(argv[1],"rt");
  32.           if (file==NULL)
  33.           {
  34.                cprintf("\nFile %s not found . . .\n",argv[1]);
  35.           }
  36.           else
  37.           {
  38.                rewind(file);
  39.                initialize(&listptr);
  40.                linenum=1;
  41.                while (fgets(line,maxstring,file)!=NULL)
  42.                {
  43.                     removelineends(line,maxstring);
  44.                     add(&listptr,line,linenum);
  45.                     linenum++;
  46.                }
  47.                numlines=linenum--;
  48.                fclose(file);
  49.                posptr=listptr;
  50.                gettextinfo(&info);
  51.                if (argc>2)
  52.                {
  53.                     getcolors(&fore,&back,argv[2],argv[3]);
  54.                }
  55.                else
  56.                {
  57.                     fore=LIGHTGRAY;
  58.                     back=BLACK;
  59.                }
  60.                height=info.screenheight;
  61.                width=info.screenwidth;
  62.                strcpy(bottomline,"  \30  \31  \33  \32  pgup  pgdn  s (search)  help (h) esc (quit)");
  63.                for(x=strlen(bottomline)+1;x<width;x++)
  64.                {
  65.                     strcat(bottomline," ");
  66.                }
  67.                _setcursortype(_NOCURSOR);
  68.                clrscr();
  69.                textcolor(back);
  70.                textbackground(fore);
  71.                gotoxy(1,height);
  72.                cputs(bottomline);
  73.                textcolor(fore);
  74.                textbackground(back);
  75.                height--;
  76.                linenum=1;
  77.                stringpos=0;
  78.                writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
  79.                ch=space;
  80.                while (ch!=escape)
  81.                {
  82.                     while ((ch!=up) && (ch!=down) && (ch!=pageup) &&
  83.                            (ch!=pagedown) && (ch!=escape) && (ch!=left) &&
  84.                            (ch!=right) && (ch!=end_) && (ch!=home) &&
  85.                            (ch!=search) && (ch!=help))
  86.                     {
  87.                          ch=getch();
  88.                          if (ch==nul)
  89.                          {
  90.                               ch=getch();
  91.                          }
  92.                     }
  93.                     if (ch==up)
  94.                     {
  95.                          if ((linenum-1)>=1)
  96.                          {
  97.                               linenum--;
  98.                          }
  99.                          else
  100.                          {
  101.                               linenum=1;
  102.                          }
  103.                          setpos(&posptr,linenum);
  104.  
  105.                     }
  106.                     else if (ch==down)
  107.                     {
  108.                          if ((linenum+1+height)<=numlines)
  109.                          {
  110.                               linenum++;
  111.                          }
  112.                          else
  113.                          {
  114.                               linenum=numlines-height+1;
  115.                          }
  116.                          setpos(&posptr,linenum);
  117.                     }
  118.                     else if (ch==pageup)
  119.                     {
  120.                          if ((linenum-height+1)>=1)
  121.                          {
  122.                               linenum=linenum-height+1;
  123.                          }
  124.                          else
  125.                          {
  126.                               linenum=1;
  127.                          }
  128.                          setpos(&posptr,linenum);
  129.                     }
  130.                     else if (ch==pagedown)
  131.                     {
  132.                          if (((linenum+height-1)<=numlines) && ((numlines-(linenum+height-1))>=(height-1)))
  133.                          {
  134.                               linenum=linenum+height-1;
  135.                          }
  136.                          else
  137.                          {
  138.                               linenum=numlines-height+1;
  139.                          }
  140.                          setpos(&posptr,linenum);
  141.                     }
  142.                     else if (ch==left)
  143.                     {
  144.                          if (stringpos>0)
  145.                          {
  146.                               stringpos--;
  147.                          }
  148.                     }
  149.                     else if (ch==right)
  150.                     {
  151.                          if (stringpos<=(maxstring-width))
  152.                          {
  153.                               stringpos++;
  154.                          }
  155.                     }
  156.                     else if (ch==end_)
  157.                     {
  158.                          stringpos=maxstring-width+1;
  159.                     }
  160.                     else if (ch==home)
  161.                     {
  162.                          stringpos=0;
  163.                     }
  164.                     else if (ch==help)
  165.                     {
  166.                          displayhelp(fore,width,height);
  167.                     }
  168.                     else if (ch==search)
  169.                     {
  170.                          readstring(searchstring,bottomline,height,fore,back);
  171.                     }
  172.                     if (ch!=escape)
  173.                     {
  174.                          ch=space;
  175.                     }
  176.                     writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
  177.                }
  178.                while (!empty(listptr))
  179.                {
  180.                     remove(&listptr);
  181.                }
  182.                height++;
  183.                window(1,1,width,height);
  184.                clrscr();
  185.                _setcursortype(_NORMALCURSOR);
  186.           }
  187.      }
  188.      return(0);
  189. }
  190.  
  191. This program should exit such that the screen is clear with the DOS prompt
  192. in the upper left corner, but no, it exits with a short line of text on the
  193. first line and then the DOS prompt on the next line. I never (or rarely)
  194. have these sort of problems with Pascal so why do they continuously crop up
  195. with C.
  196.  
  197. PROBLEM 2 :
  198.  
  199. const escape=27;
  200. const cr=13;
  201. const bs=8;
  202.  
  203. while (ch!=cr)
  204. {
  205.      .
  206.      .
  207.      .
  208. }
  209.  
  210. while (ch!=escape)
  211. {
  212.      .
  213.      .
  214.      .
  215. }
  216.  
  217. If I replace the 27 with \27' or the 13 with '\13' then these loops don't
  218. work i.e. an infinite loop results. WHY?
  219.  
  220. Also if I want to do somthing like this : puts("\24 \25"); which should
  221. print the up and down arrows on the screen (ASCII 24 and 25), but no, it
  222. prints some other ASCII characters. As far as I can tell C has its own
  223. unique character table, at least for the control characters. WHY?
  224.  
  225. Sometimes I wonder whether some one needs to go back an rewrite the damn
  226. language so that it works sensibly and predictably like Pascal can be
  227. relied upon to do.
  228.